#include "get_adc.h"


void get_4_adc(uint16_t adc1[],power *p){
	   float adc_value[4];
	   bias adc;
	   init_bias(&adc);
     average_filter(adc1,adc_value);
     adc_value[0]=adc_value[0]/(0xffff)*3.3*(adc.adc1_k)+adc.adc1_bias;
	   adc_value[1]=adc_value[1]/(0xffff)*3.3*(adc.adc2_k)+adc.adc2_bias;
	   adc_value[2]=adc_value[2]/(0xffff)*3.3*(adc.adc3_k)+adc.adc3_bias;
     adc_value[3]=adc_value[3]/(0xffff)*3.3*(adc.adc4_k)+adc.adc4_bias; 		
		 get_power(p,adc_value);
}

void get_chassis_adc(uint16_t adc1[],power *p){
	   float adc_value[1],current_chassis;
	   bias adc;
	   init_bias(&adc);
     average_filter_single(adc1,adc_value);
     current_chassis=adc_value[0]/(0xffff)*3.3*(adc.adc5_k)+adc.adc5_bias;	
		 p->chassis_power=current_chassis*(p->battery_voltage-0.7);
}

void set_adc(uint16_t adc1[],bias *adc,float adcvalue[]){

	   float adc_value[4];
		 adc_value[0]=adc1[0]/(0xffff)*3.3*(adc->adc1_k)+adc->adc1_bias;
	   adc_value[1]=adc1[1]/(0xffff)*3.3*(adc->adc2_k)+adc->adc2_bias;
	   adc_value[2]=adc1[2]/(0xffff)*3.3*(adc->adc3_k)+adc->adc3_bias;
     adc_value[3]=adc1[3]/(0xffff)*3.3*(adc->adc4_k)+adc->adc4_bias; 	 
}

void init_bias(bias *adc){
    adc->adc1_bias=ad1_bias;
	  adc->adc2_bias=ad2_bias;
	  adc->adc3_bias=ad3_bias;
	  adc->adc4_bias=ad4_bias;
	  adc->adc5_bias=ad5_bias
    adc->adc1_k=ad1_k;
	  adc->adc2_k=ad2_k;
	  adc->adc3_k=ad3_k;
	  adc->adc4_k=ad4_k;
	  adc->adc5_k=ad5_k;
}
void get_power(power *p,float adcvalue[]){
   p->battery_current=adcvalue[2];
	 p->battery_voltage=adcvalue[0];
	 p->battery_power=adcvalue[2]*adcvalue[0];
   p->cap_current=adcvalue[1];
	 p->cap_voltage=adcvalue[3];
	 p->cap_power=adcvalue[1]*adcvalue[3];
}
